<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $table = 'user';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'email', 'type', 'pwd', 'org_id', 'phone', 'avatars', 'team_id', 'open_id','platform_id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'pwd', 'remember_token',
    ];

    public function getAuthPassword() {
        return $this->pwd;
    }

    public function volunteer() {
        return $this->hasOne('App\Models\Volunteer', 'user_id', 'id');
    }

    public function org() {
        return $this->hasOne('App\Models\Org', 'id', 'org_id');
    }

    public function platform() {
        return $this->hasOne('App\Models\Platform', 'id', 'platform_id');
    }

    public function team() {
        return $this->hasOne('App\Models\Team', 'id', 'team_id');
    }

    public function inbox() {
        return $this->hasMany('App\Models\Message', 'to_user_id', 'id');
    }

    public function outbox() {
        return $this->hasMany('App\Models\Message', 'from_user_id', 'id');
    }

    public function getAclList() {
        $list = [];
        if ($this->type == 5) {
            $list[] = 'home:home';
            $list[] = 'banner:*';
            $list[] = 'user:edit_own_platform';
            $list[] = 'org:view';
            $list[] = 'org:admin';
            $list[] = 'team:view';
            $list[] = 'team:list';
            $list[] = 'project:view';
            $list[] = 'project:auth';
            $list[] = 'project:list';
            $list[] = 'volunteer:list';
//            $list[] = 'volunteer:import';
//            $list[] = 'volunteer:blacklist';
            $list[] = 'volunteer:view';
//            $list[] = 'volunteer:edit';
            $list[] = 'volunteer:team';
//            $list[] = 'volunteer:project';
//            $list[] = 'volunteer:family';
//            $list[] = 'statistic:volunteer';
//            $list[] = 'statistic:team';
            $list[] = 'news:list';
        } elseif($this->type == 4) {
            $list[] = 'home:home';
            $list[] = 'user:msg';
            $list[] = 'module:*';
            $list[] = 'banner:*';
            $list[] = 'org:*';
            $list[] = 'settings:*';
            $list[] = 'team:*';
            $list[] = 'project:*';
            $list[] = 'volunteer:list';
            $list[] = 'volunteer:import';
            $list[] = 'volunteer:blacklist';
            $list[] = 'volunteer:view';
            $list[] = 'volunteer:edit';
            $list[] = 'volunteer:team';
            $list[] = 'volunteer:project';
            $list[] = 'volunteer:family';
            $list[] = 'third:*';
            $list[] = 'good_example';
            $list[] = 'claim_project';
            $list[] = 'safe_plan:*';
            $list[] = 'feeling_tone:list';
            $list[] = 'feeling_tone:comment';
            $list[] = 'feeling_tone:delete';
            $list[] = 'finance:order';
            $list[] = 'finance:account_list';
            $list[] = 'finance:view';
            $list[] = 'finance:cash';
            $list[] = 'star_volunteer:list';
            $list[] = 'star_team:list';
            $list[] = 'statistic:volunteer';
            $list[] = 'statistic:team';
            $list[] = 'statistic:org';
            $list[] = 'news:list';
            $list[] = 'score_mall:banner';
            $list[] = 'score_mall:list';
            $list[] = 'score_mall:merchant';
            $list[] = 'platform:admin';
        } else if ($this->type == 3) {
            $list[] = 'home:home';
            $list[] = 'user:edit_own_org';
            $list[] = 'user:msg';
            $list[] = 'banner:*';
            $list[] = 'team:*';
            $list[] = 'project:*';
            $list[] = 'skip:select-org';
            $list[] = 'volunteer:list';
            $list[] = 'volunteer:import';
            $list[] = 'volunteer:blacklist';
            $list[] = 'volunteer:view';
            $list[] = 'volunteer:edit';
            $list[] = 'volunteer:team';
            $list[] = 'volunteer:project';
            $list[] = 'volunteer:family';
            $list[] = 'safe_plan:buy';
            $list[] = 'finance:order';
            $list[] = 'finance:account_list';
            $list[] = 'finance:view';
            $list[] = 'finance:cash';
            $list[] = 'statistic:volunteer';
            $list[] = 'statistic:org';
            $list[] = 'statistic:team';
            $list[] = 'news:list';
            $list[] = 'score_mall:banner';
            $org = $this->org;
            if (isset($org->open_stars) && $org->open_stars) {
                $list[] = 'star_team:list';
                $list[] = 'star_volunteer:list';
            }
            if (isset($org->module_settings)) {
                foreach ($org->module_settings as $modules) {
                    foreach ($modules as $module) {
                        if ($module['key'] === 'volunteer_feedback') {
//                            $list[] = 'third:feedback';
                            $list[] = 'score_mall:list';
                            $list[] = 'score_mall:merchant';
                        } else if ($module['key'] === 'good_example') {
                            $list[] = 'good_example';
                        } else if ($module['key'] === 'claim_project') {
                            $list[] = 'claim_project';
                        } else if ($module['key'] === 'community_interact') {
                            $list[] = 'feeling_tone:list';
                            $list[] = 'feeling_tone:comment';
                            $list[] = 'feeling_tone:delete';
                        }/* else if ($module['key'] === 'volunteer_strategy') {
                            $list[] = 'third:strategy';
                        }*/
                    }
                }
            }
        } else if ($this->type == 2) {
            if ($this->team->in_blacklist) {
                $list[] = 'notify:blacked';
            } else if ($this->team && $this->team->verify_status == 1) {
                $list[] = 'home:home';
                $list[] = 'user:edit_own_team';
                $list[] = 'user:msg';
                $list[] = 'user:edit_team_setting';
                $list[] = 'skip:select-org';
                $list[] = 'skip:select-team';
                $list[] = 'volunteer:verify';
                $list[] = 'volunteer:my';
                $list[] = 'volunteer:group'; // 成员分组
                $list[] = 'volunteer:edit';
                $list[] = 'volunteer:team';
                $list[] = 'volunteer:project';
                $list[] = 'project:commit';
                $list[] = 'project:create';
                $list[] = 'project:auth';
                $list[] = 'project:my';
                $list[] = 'project:edit';
                $list[] = 'project:verify_reward_time';
                $list[] = 'safe_plan:buy';
                $list[] = 'finance:order';
                $list[] = 'finance:setting';
                $list[] = 'finance:apply';
                $list[] = 'finance:history';
                $list[] = 'statistic:volunteer';
                $org = $this->team->org;
                if ($this->team->auth_create_team) {
                    $list[] = 'team:*';
                    $list[] = 'volunteer:import';

                    if (isset($org->module_settings)) {
                        foreach ($org->module_settings as $modules) {
                            foreach ($modules as $module) {
                                if ($module['key'] === 'community_interact') {
                                    $list[] = 'feeling_tone:list';
                                    $list[] = 'feeling_tone:qrcode';
                                    $list[] = 'feeling_tone:replies';
                                    $list[] = 'feeling_tone:reply';
                                }
                            }
                        }
                    }
                }
                if (isset($org->open_stars) && $org->open_stars) {
                    $list[] = 'star_volunteer:list';
                }
                if ($this->team->auth_exempt_project) {
                    $list[] = 'project:verify';
                    $list[] = 'project:list';
                } else {
                    $list[] = 'project:pending';
                    $list[] = 'project:draft';
                }

                if ($this->team->auth_create_goods) {
                    $list[] = 'score_mall:list';
                }
            } else if ($this->team && $this->team->verify_status == 0){ // 新注册还没通过审核
                $list[] = 'home:home';
                $list[] = 'user:edit_own_team';
                $list[] = 'user:edit_team_setting';
                $list[] = 'skip:select-org';
                $list[] = 'skip:select-team';
                $list[] = 'project:pending';
                $list[] = 'project:draft';
                $list[] = 'project:create';
                $list[] = 'project:edit';
                if ($this->team && $this->team->verify_status == -1) {
                    $list[] = 'notify:complete_info';
                } else {
                    $list[] = 'notify:unverified';
                }
            } else {
                $list[] = 'notify:complete_info';
                $list[] = 'user:edit_own_team';
            }
        }
        return $list;
    }
}
